package com.qf.ayc.proimgs.controller;

import com.qf.ayc.product.entity.Product;
import com.qf.ayc.product.service.IProductService;
import com.qf.ayc.proimgs.entity.Proimgs;
import com.qf.ayc.proimgs.service.IProimgsService;
import com.qf.ayc.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author BeautyFairy
 * @since 2024-03-01
 */
@RestController
@RequestMapping("/proimgs")

@CrossOrigin
public class ProimgsController {
    @Autowired
    private IProimgsService iProimgsService;
    @Autowired
    private IProductService iProductService;
    @PostMapping("/add")
    public ResultVo add(@RequestBody Proimgs proimgs){
        Integer proid = proimgs.getProid();
        Product byId = iProductService.getById(proid);
        if(byId!=null){
            boolean save = iProimgsService.save(proimgs);
            if(save){
                return ResultVo.ok("1","添加商品图片成功",proimgs);
            }else{
                return ResultVo.ok("-1","添加商品图片失败");
            }
        }else{
            return ResultVo.ok("-1","没找到对应商品id，请检查图片对应的商品id是否正确",proimgs);
        }
    }
    @GetMapping("/del")
    public ResultVo del(String id){
        Proimgs byId = iProimgsService.getById(id);
        if(byId!=null){
            boolean b = iProimgsService.removeById(byId);
            if(b){
                return ResultVo.ok("1","删除商品图片成功");
            }else{
                return ResultVo.ok("-1","删除商品图片失败");
            }
        }else{
            return ResultVo.ok("-1","没有找到此商品图片");
        }
    }
}
